package com.example.ioc;

import com.example.ioc.service.impl.OrderServiceImpl;
import com.example.ioc.service.impl.UserServiceImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        ConfigurableApplicationContext ioc = SpringApplication.run(Application.class, args);
        System.out.println("userServiceImpl="+ioc.getBean("userServiceImpl"));
        /**
         * 验证的是orderServiceImpl中加强的getObject
         */
        // com.example.ioc.service.impl.UserServiceImpl@39e67516
        System.out.println("orderServiceImpl="+ioc.getBean("orderServiceImpl"));
        /**
         * 要本身的bean对象
         * 1、&+beanName
         * 2、对象类型
         */
        // com.example.ioc.service.impl.OrderServiceImpl@77010a30
        System.out.println("&orderServiceImpl="+ioc.getBean("&orderServiceImpl"));
        // com.example.ioc.service.impl.OrderServiceImpl@77010a30
        System.out.println("OrderServiceImpl.class="+ioc.getBean(OrderServiceImpl.class));
        /**
         * 如果不实现 getObjectType 返回 UserService.class
         * 则报错： No qualifying bean of type 'com.example.ioc.service.UserService' available
         *
         * 如果只实现getObjectType 返回 UserService.class
         * 则 ioc.getBean(UserService.class) == ioc.getBean("orderServiceImpl")
         *
         * 如果配置或UserServiceImpl配置Bean并且使用@Primary指定为主要 bean，优先使用，
         * ioc.getBean(UserService.class) 不再等于 ioc.getBean("orderServiceImpl")
         * 则 ioc.getBean(UserService.class) == ioc.getBean("userService")
         *
         */
        // com.example.ioc.service.impl.UserServiceImpl@28348c6
        System.out.println("UserServiceImpl.class="+ioc.getBean(UserServiceImpl.class));
        /**
         * 直接使用beanName由于这时容器中的userService.class是通过OrderServiceImpl中伪装进行的，不确定是否存在该beanName
         * 则会报错： No bean named 'userService' available
         *
         * 如果将  UserServiceImpl 中打开 @Service 会出现bean重复
         * 则报错： No qualifying bean of type 'com.example.ioc.service.UserService' available: expected single matching bean but found 2: orderServiceImpl,userServiceImpl
         * System.out.println(ioc.getBean("userService"));
         *
         * 可以通过配置类，配置一个指定的beanName 并且由于orderServiceImpl对注入了一个UserServiceImpl的bean对象
         * 所以在配置类中 用使用@Primary指定为主要 bean
         * 也会报错： No qualifying bean of type 'com.example.ioc.service.UserService' available: expected single matching bean but found 2: orderServiceImpl,userService
         *
         * 当前也可以在 UserServiceImpl中使用注解并且声明beanName @Service(value = "userService") 和 @Primary
         */
        // com.example.ioc.service.impl.UserServiceImpl@28348c6
        System.out.println("userServiceImpl="+ioc.getBean("userServiceImpl"));
    }

}
